package com.abbcc.module.admin;

import java.util.Date;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.abbcc.action.BaseAction;
import com.abbcc.common.CommonConst;
import com.abbcc.models.TdUser;
import com.abbcc.service.UserService;
import com.abbcc.util.ObjectUtil;
import com.abbcc.util.StringUtil;

public class UserAction extends BaseAction<TdUser> {
	@Autowired
	private UserService userService;
	public String searchKey;
	public int[] userIds;
	public String clickName;

	public String list() {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (StringUtil.isNotBlank(searchKey)) {
			dc.add(Restrictions.or(Restrictions.like("username", searchKey,
					MatchMode.ANYWHERE), Restrictions.like("name", searchKey,
					MatchMode.ANYWHERE)));
		}
		dc.addOrder(Order.desc("userId"));
		pageList = userService.findPageByCriteria(dc, DEFAULT_PAGESIZE,
				startIndex);
		saveLog("员工管理", StringUtil.isNotBlank(searchKey)?("查询："+searchKey):"查询全部", CommonConst.LOGFIND, CommonConst.LOGTYPEBYADMIN);
		return LIST;
	}

	public String save() {
		if (isRepeat(entity.getUsername())) {
			entity.setAddTime(new Date());
			entity.setToDelete("0");
			entity.setToUpdate("0");
			entity.setToWrite("0");
			userService.save(entity);
			result = "添加成功";
		} else {
			result = "添加失败，该用户名：" + entity.getUsername() + "已经存在!";
		}
		saveLog("员工:"+entity.getUsername(), result, CommonConst.LOGSAVE, CommonConst.LOGTYPEBYADMIN);
		return list();
	}

	public String edit() {
		//deposit("users", getEntityById());
		userService.load(entity, entity.getUserId());
		return EDIT;
	}

	public String update() {
		TdUser td = getEntityById();
		if (!entity.getUsername().equals(td.getUsername())) {
			if (!isRepeat(entity.getUsername())) {
				result = "添加失败,您添加的账号已经存在";
				saveLog("员工:"+entity.getUsername(), result, CommonConst.LOGUPDATE, CommonConst.LOGTYPEBYADMIN);
				return list();
			}
		}
		if (StringUtil.isBlank(entity.getPassword())) {
			entity.setPassword(td.getPassword());
		}
		ObjectUtil.extend(td, entity);
		userService.update(td);
		result = "修改成功!";
		saveLog("员工:"+entity.getUsername(), result, CommonConst.LOGUPDATE, CommonConst.LOGTYPEBYADMIN);
		return list();
	}

	public String del() {
		baseService.delete(baseService.findById(entityClass, entity.getUserId()));
		result = "删除成功!";
		saveLog("员工:"+entity.getUsername(), result, CommonConst.LOGDEL, CommonConst.LOGTYPEBYADMIN);
		return list();
	}

	public String allDel() {
		if(userIds!=null&&userIds.length!=0){
			String name = "";
			for(int uId:userIds){
				TdUser td = (TdUser) baseService.findById(entityClass, uId);
				name +=(td.getUsername()+",");
				baseService.delete(td);
			}
			result = "删除成功!";
			saveLog("员工:"+name, result, CommonConst.LOGDEL, CommonConst.LOGTYPEBYADMIN);
		}
		return list();
	}
	//修改权限
	public String permissions(){
		TdUser td = getEntityById();
		if(StringUtil.isNotBlank(clickName)){
			String logname="";
			if(clickName.equals("write")){
				String t = StringUtil.isBlank(td.getToWrite())?"1":td.getToWrite().equals("1")?"0":"1";
				td.setToWrite(t);
				logname = (t.equals("1")?"打开":"关闭")+"添加权限";
			}
			if(clickName.equals("update")){
				String t = StringUtil.isBlank(td.getToUpdate())?"1":td.getToUpdate().equals("1")?"0":"1";
				td.setToUpdate(t);
				logname = (t.equals("1")?"打开":"关闭")+"修改权限";
			}
			if(clickName.equals("delete")){
				String t = StringUtil.isBlank(td.getToDelete())?"1":td.getToDelete().equals("1")?"0":"1";
				td.setToDelete(t);
				logname = (t.equals("1")?"打开":"关闭")+"删除权限";
			}
			userService.update(td);
			saveLog("员工:"+td.getUsername(), logname, CommonConst.LOGUPDATE, CommonConst.LOGTYPEBYADMIN);
		}
		return JSON;
	}
	//还原密码
	public String toInit(){
		TdUser td = getEntityById();
		td.setPassword("123456");
		userService.update(td);
		saveLog("员工:"+td.getUsername(), "密码还原", CommonConst.LOGUPDATE, CommonConst.LOGTYPEBYADMIN);
		return JSON;
	}
	private TdUser getEntityById() {
		return (TdUser) baseService.findById(entityClass, entity.getUserId());
	}

	private boolean isRepeat(String username) {
		TdUser t = new TdUser();
		t.setUsername(entity.getUsername());
		return userService.findByExample(t).isEmpty();
	}

	public String getSearchKey() {
		return searchKey;
	}

	public void setSearchKey(String searchKey) {
		this.searchKey = searchKey;
	}
	

}
